package com.wilmer.qiniuyun.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * @author hans
 * 2024-3-31
 */
public class UniqueTimestampFileNameGeneratorUtil {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");

    public static String generateUniqueFileName(String originalFileName) {
        String timestamp = dateFormat.format(new Date());
        String extension = getFileExtension(originalFileName);

        return timestamp + extension;
    }

    private static String getFileExtension(String fileName) {
        int lastDotIndex = fileName.lastIndexOf('.');
        if (lastDotIndex > 0 && lastDotIndex < fileName.length() - 1) {
            return fileName.substring(lastDotIndex);
        }
        return "";
    }
}

